package com.genghis.ptas.question.service.impl;

import com.genghis.core.page.entity.Page;
import com.genghis.core.page.entity.PageParameter;
import com.genghis.ptas.classify.service.ClassifyService;
import com.genghis.ptas.constant.DictConstant;
import com.genghis.ptas.question.dao.QuestionDao;
import com.genghis.ptas.question.entity.Answer;
import com.genghis.ptas.question.entity.Question;
import com.genghis.ptas.question.service.AnswerService;
import com.genghis.ptas.question.service.QuestionService;
import com.genghis.ptas.security.entity.User;
import com.genghis.ptas.security.service.UserService;
import com.genghis.ptas.system.service.DictService;
import com.genghis.ptas.util.DateUtil;
import com.genghis.ptas.util.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: gaoyang
 * To change this template use File | Settings | File Templates.
 */
@Service("questionService")
public class QuestionServiceImpl implements QuestionService {
    @Autowired
    private AnswerService answerService;

    @Autowired
    private QuestionDao questionDao;

    @Autowired
    private UserService userService;

    @Autowired
    private DictService dictService;

    @Override
    public Page<Question> findAllQuestionPageList(PageParameter pageParameter, Question question) {
        Page<Question> questionPage = questionDao.findAllQuestionPageList(pageParameter, question);
        for (Question questionTemp : questionPage.getResultList()) {
            questionTemp.setAskDate(questionTemp.getAskDate().substring(0, 9));
            questionTemp.setUserName(userService.findUserAllInfoById(questionTemp.getUserId()).getUserName());
        }
        return questionPage;
    }

    @Override
    public Page<Answer> findAllAnswerPageList(PageParameter pageParameter, Answer answer) {
        Page<Answer> answerPage = questionDao.findAllAnswerPageList(pageParameter, answer);
        for (Answer answerTemp : answerPage.getResultList()) {
            answerTemp.setAnswerDate(answerTemp.getAnswerDate().substring(0, 9));
            answerTemp.setUserName(userService.findUserAllInfoById(answerTemp.getUserId()).getUserName());
            answerTemp.setShowIsAdopted(dictService.changeDictCodeToValue(answerTemp.getIsAdopted(), DictConstant.IS_ADOPTED));
        }
        return answerPage;
    }

    @Override
    public List<Question> findAllQuestionByContent(String content) {
        List<Question> questionList = questionDao.findAllQuestionByContent(content);
        for(Question question : questionList){
            List<Answer> answer = answerService.findNewestAnswerByQuestionId(question.getId());
            question.setAnswer(answer.size()==0? new Answer() : answer.get(0));
        }
        return questionList;
    }

    @Override
    public void addQuestion(Question question) {
        String loginName = LoginUserUtil.findLoginUserName();
        User user = userService.findUserByLoginName(loginName);
        question.setState(0);
        question.setAskDate(DateUtil.getSystemDate(DictConstant.DATE_FORMAT));
        question.setUserId(user.getId());
        questionDao.addQuestion(question);
    }

    @Override
    public void editQuestion(Question question) {
        Question oldQuestion = findQuestionById(question.getId());
        oldQuestion.setQuestionContent(question.getQuestionContent());
        oldQuestion.setQuestionType(question.getQuestionType());
        questionDao.editQuestion(oldQuestion);
    }

    @Override
    public Question findQuestionById(int id) {
        return questionDao.findQuestionById(id);
    }

    @Override
    public void delAskByIds(String ids) {
        answerService.delAnswerByQuestionId(ids);
        questionDao.delAskByIds(ids);
    }
}
